﻿using Manage.Traceability.Api.DTO;
using Manage.Traceability.Domain;
using Manage.Traceability.ErrorCode;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace Manage.Traceability.Api.Controllers
{
    /// <summary>
    /// 屠宰控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class SlaughterController : ControllerBase
    {
        private readonly ISqlSugarClient db;

        public SlaughterController(ISqlSugarClient db)
        {
            this.db = db;
        }
        /// <summary>
        /// 显示屠宰信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ApiResult<SlaughterDTO> ShowSlaughter()
        {
            var list = db.Queryable<SlaughterModel>().Where(x => x.SlaughterIsDeleted == false)
               .Select(x => new SlaughterDTO
               {
                   SlaughterId=x.SlaughterId,
                   SlaughterCode = x.SlaughterCode,
                   SlaughterMess = x.SlaughterMess,
                   SlaughterEndTime = x.SlaughterEndTime,
                   SlaughterBetTime = x.SlaughterBetTime,
               }).OrderByDescending(x => x.SlaughterId).Take(1).First();
            return ApiResult<SlaughterDTO>.Success("显示养殖信息", list);
        }
        /// <summary>
        /// 添加屠宰信息
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public ApiResult<int> AddFarming(SlaughterDTO dto)
        {
            SlaughterModel slaug = new SlaughterModel();
            slaug.SlaughterCode=dto.SlaughterCode;
            slaug.SlaughterMess=dto.SlaughterMess;
            slaug.SlaughterBetTime=dto.SlaughterBetTime;
            slaug.SlaughterEndTime=dto.SlaughterEndTime;
            int r1 = db.Insertable(slaug).ExecuteCommand(); //都是参数化实现
            if (r1 > 0)
            {
                return ApiResult<int>.Success("添加屠宰信息成功", r1);
            }
            else
            {
                return ApiResult<int>.Fail("添加屠宰信息失败");
            }
        }
        /// <summary>
        /// 显示省份
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ApiResult<List<ProvinceModel>> GetProvince()
        {
              List<ProvinceModel> list = db.Queryable<ProvinceModel>().ToList();
              return ApiResult<List<ProvinceModel>>.Success("查询省份成功", list);
        }
        /// <summary>
        /// 显示市级
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ApiResult<List<CityModel>> GetCity(int id)
        {
            List<CityModel> list = db.Queryable<CityModel>().Where(x => x.ProvincId == id).ToList();
            return ApiResult<List<CityModel>>.Success("查询成功", list);
        }
        /// <summary>
        /// 显示县级
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ApiResult<List<CountyModel>> GetCounty(int id)
        {
            List<CountyModel> list = db.Queryable<CountyModel>().Where(x => x.CityId == id).ToList();
            return ApiResult<List<CountyModel>>.Success("查询成功", list);
        }
        /// <summary>
        /// 显示屠宰企业信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ApiResult<SlaugDTO>ShowSlaug()
        {
            var query5 = db.Queryable<SlaugModel>()
             .LeftJoin<ProvinceModel>((a, b) => a.ProvincId == b.ProvincId)//多个条件用&&
             .LeftJoin<CityModel>((a,b,c) => a.CityId == c.CityId)//多个条件用&&
             .LeftJoin<CountyModel>((a,b,c,d) => a.CountyId==d.CountyId)//多个条件用&&
             .Select((a, b,c,d) => new SlaugDTO
             {
                 SlaughterId=a.SlaughterId,
                 SlaughterName=a.SlaughterName,
                 ProvincId=a.ProvincId,
                 ProvincName = b.ProvincName,
                 CityId =a.CityId,
                 CityName=c.CityName,
                 CountyId =a.CountyId,
                 CountyName=d.CountyName,
                 SlaughterTime = a.SlaughterTime,
                 SlaughterHome = a.SlaughterHome
             }).OrderByDescending(a => a.SlaughterId).Take(1).First();
            return ApiResult<SlaugDTO>.Success("查询屠宰企业信息成功",query5);
        }
        /// <summary>
        /// 添加屠宰企业信息
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public ApiResult<int>AddSlaug(SlaughugsDTO dto)
        {
            SlaugModel slaugs= new SlaugModel(); 
            slaugs.SlaughterName=dto.SlaughterName;
            slaugs.ProvincId=dto.ProvincId;
            slaugs.CityId=dto.CityId;
            slaugs.CountyId=dto.CountyId;
            slaugs.SlaughterTime=dto.SlaughterTime;
            slaugs.SlaughterHome=dto.SlaughterHome;
            int r1 = db.Insertable(slaugs).ExecuteCommand(); //都是参数化实现
            if (r1 > 0)
            {
                return ApiResult<int>.Success("添加屠宰企业信息成功", r1);
            }
            else
            {
                return ApiResult<int>.Fail("添加屠宰企业信息失败");
            }
        }
    }
}